<?php

use yii\db\Migration;
use yii\db\Schema;

/**
 * Class m180928_041042_create_db
 */
class m180928_041042_create_db extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {

        $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';


        // 客户联系人
        $this->createTable('customer_contact', [
            "id" =>             Schema::TYPE_PK,
            "product_id" =>     Schema::TYPE_INTEGER.   "(10) NOT NULL DEFAULT '0' COMMENT '团队ID'",

            "customer_id" =>     Schema::TYPE_INTEGER.   "(10) NOT NULL DEFAULT '0' COMMENT '团队ID'",

            "p_name" =>        Schema::TYPE_STRING. "(128)  NULL COMMENT '联系人姓名'",
            "p_phone" =>        Schema::TYPE_STRING. "(128)  NULL COMMENT '联系人电话'",
            "p_mail" =>        Schema::TYPE_STRING. "(128)  NULL COMMENT '联系人邮箱'",
            "p_department" =>        Schema::TYPE_STRING. "(128)  NULL COMMENT '联系人部门'",

            "status" =>         Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1正常  2 禁用 '",

            "user_id" =>        Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '创建人ID'",

            "created_at" =>     Schema::TYPE_DATETIME. " NULL COMMENT '创建时间'",
            "updated_at" =>     Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);



        // invoice
        $this->createTable('invoice', [
            "id" =>             Schema::TYPE_PK,
            "product_id" =>     Schema::TYPE_INTEGER.   "(10) NOT NULL DEFAULT '0' COMMENT '团队ID'",

            "code" =>     Schema::TYPE_STRING.  "(32) NOT NULL  COMMENT 'invoice编码'",

            "currency" =>   Schema::TYPE_INTEGER. "(2) DEFAULT '1' COMMENT '货币(美元)'",

            "price" => Schema::TYPE_DECIMAL.   "(10,2) DEFAULT '0.00' COMMENT '金额' ",

            "download_time" =>     Schema::TYPE_DATETIME. " NULL COMMENT '下载时间'",

            "status" =>         Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1正常  2 禁用 '",

            "user_id" =>        Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '创建人ID'",

            "created_at" =>     Schema::TYPE_DATETIME. " NULL COMMENT '创建时间'",
            "updated_at" =>     Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);

        // 收款表
        $this->createTable('income', [
            "id" =>             Schema::TYPE_PK,
            
            "company" =>       Schema::TYPE_STRING. "(128)  NULL COMMENT '公司'",

            "currency" =>   Schema::TYPE_INTEGER. "(2) DEFAULT '1' COMMENT '货币(美元)'",

            "price" => Schema::TYPE_DECIMAL.   "(10,2) DEFAULT '0.00' COMMENT '收入金额' ",

            "status" =>         Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1正常  2 禁用 '",

            "user_id" =>        Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '创建人ID'",

            "created_at" =>     Schema::TYPE_DATETIME. " NULL COMMENT '创建时间'",
            "updated_at" =>     Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);

         // 收款认领表
        $this->createTable('income_claim', [
            "id" =>             Schema::TYPE_PK,
            
            "product_id" =>     Schema::TYPE_INTEGER.   "(10) NOT NULL DEFAULT '0' COMMENT '团队ID'",

            "income_id" =>     Schema::TYPE_INTEGER.   "(10) NOT NULL DEFAULT '0' COMMENT '收款ID'",

            "currency" =>   Schema::TYPE_INTEGER. "(2) DEFAULT '1' COMMENT '货币(美元)'",

            "price" => Schema::TYPE_DECIMAL.   "(10,2) DEFAULT '0.00' COMMENT '认领金额' ",

            "status" =>         Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1正常  2 禁用 '",

            "user_id" =>        Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '创建人ID'",

            "created_at" =>     Schema::TYPE_DATETIME. " NULL COMMENT '创建时间'",
            "updated_at" =>     Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);


         // 团队导游表
        $this->createTable('product_tour', [
            "id" =>             Schema::TYPE_PK,
            
            "product_id" =>     Schema::TYPE_INTEGER.   "(10) NOT NULL DEFAULT '0' COMMENT '团队ID'",

            "day_date" =>      Schema::TYPE_DATE. " NOT NULL COMMENT '日期' ",

            "city" =>        Schema::TYPE_STRING. "(255) NULL COMMENT '目的地城市'",

            "tour_id" =>   Schema::TYPE_STRING. "(255) NULL COMMENT '指定导游(多个)'",

            "status" =>         Schema::TYPE_INTEGER.   "(1) DEFAULT  '1' COMMENT '1正常  2 禁用 '",

            "user_id" =>        Schema::TYPE_INTEGER.   "(10) DEFAULT '0' COMMENT '创建人ID'",

            "created_at" =>     Schema::TYPE_DATETIME. " NULL COMMENT '创建时间'",
            "updated_at" =>     Schema::TYPE_TIMESTAMP. " NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
        ], $tableOptions);

    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        echo "m180928_041042_create_db cannot be reverted.\n";

        $this->dropTable('customer_contact');
        $this->dropTable('invoice');
        $this->dropTable('income');
        $this->dropTable('income_claim');
        $this->dropTable('product_tour');
        
        return false;
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m180928_041042_create_db cannot be reverted.\n";

        return false;
    }
    */
}
